package com.yd.controller.upfile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yd.controller.yd.YD_Can;
import com.yd.controller.yd.YD_ShanghuJiage;
import com.yd.controller.yd.YD_Sys;
import com.yd.controller.yd.YD_Users;
import com.yd.pojo.Balance;
import com.yd.pojo.Orders;
import com.yd.pojo.OrdersInfo;
import com.yd.service.BalanceService;
import com.yd.service.OrdersInfoService;
import com.yd.service.OrdersService;
import com.yd.tool.UtsBigdeciArith;
import com.yd.tool.UtsReg;

import jxl.Sheet;
import jxl.Workbook;

@Controller
public class ReadJsonExcelzijishangchuan {

	@Resource
	private OrdersService ordersService;
	@Resource
	private OrdersInfoService ordersInfoService;
	@Resource
	private BalanceService balanceService;
	@RequestMapping("/app/tianjiazijishangchuan")
	@ResponseBody
	public String createJson(HttpServletRequest request) throws MalformedURLException, IOException {

		        // 获取key token
				   // 获取管理员信息
				// 获取商户id
				   // 商户信息
				// 文件地址
				   // 循环输出单元格内容
				      // 计算应结款
				      // 根据商户id 和 省 获取一条商户价格表信息
				         // 根据计算规则计算

				      // 绑定实体类
				      // 插入数据库
				// 计算总应结款 更新余额表
		
		
		JSONObject jsonObject = new JSONObject();
		
		String token = "";
		String id = "";
		/*String name="";*/
		if (UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "token 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("id"))) {id = request.getParameter("id").trim();} else {jsonObject.put("msg", "id 错误");jsonObject.put("error", "1");return jsonObject.toString();}
/*		if (UtsReg.getStringKB(request.getParameter("name"))) {name = request.getParameter("name").trim();} else {jsonObject.put("msg", "name 错误");jsonObject.put("error", "1");return jsonObject.toString();}
*/
		
		JSONObject yd_usersys = YD_Sys.getshidByToken(token);
		if (yd_usersys.getString("error").equals("1")) {jsonObject.put("msg", yd_usersys.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token_sys = JSONObject.parseObject(yd_usersys.getString("data"));

	
		JSONObject yd_shanghu = YD_Users.getUsersByMerchantid(id);
		if (yd_shanghu.getString("error").equals("1")) {jsonObject.put("msg", yd_shanghu.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject yd_shanghu_id = JSONObject.parseObject(yd_shanghu.getString("data"));
		
		
		List<List<String>> lists=new ArrayList<List<String>>();
		int i;
		Sheet sheet;
		Workbook book;
		try {
		/*	//获取配置文件的地址
			String ex_adr=YD_Can.getUrlValue("ex_add");//F:\woke\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\duizhangtong_files_yd\ex\
			String filename=name;//asdfsdaf.xls
			book = Workbook.getWorkbook(new File(ex_adr+filename));*/
			book = Workbook.getWorkbook(new File("F:\\666.xls"));
			sheet = book.getSheet(0);
			
			i=1;
			while (true) {
				if("".equals(sheet.getCell(0, i).getContents())==true){  break;  }//如果读取的数据为空 

				List<String> list=new ArrayList<String>();
				for(int j=0;j<51;j++){
					
					String ziduan="0.0";
					if(sheet.getCell(j, i).getContents().length()>0){
						ziduan=sheet.getCell(j, i).getContents();
					}
					list.add(ziduan);
				};
				
				lists.add(list);
				i++;
			}


		book.close();
		} catch (Exception e) {}

		
		Orders orders = new Orders();
		OrdersInfo ordersInfo = new OrdersInfo();
		
		List<String> msglist=new ArrayList<String>();
		
		for (int j = 0; j < lists.size(); j++) {//相当于ex里面的100条数据
			List<String> list=lists.get(j);//第j条
			//取第几列 用   list.get(0).toString(); 0 代表第0列   
			

			
			
			orders.setYundanhao(list.get(0).toString());
			ordersInfo.setYundanhao(list.get(0).toString());
			ordersInfo.setKoukuanzhuangtai(list.get(1).toString());
			ordersInfo.setBaohao(list.get(2).toString());
			ordersInfo.setZhangdanriqi(list.get(3).toString());
			ordersInfo.setShoujiansaomiaoshijian(list.get(4).toString());
			ordersInfo.setLanjianyewuyuan(list.get(5).toString());
			ordersInfo.setGonghao(list.get(6).toString());
			ordersInfo.setChengbaoqu(list.get(7).toString());
			ordersInfo.setKehumingcheng(list.get(8).toString());
			ordersInfo.setJiesuanduixiang(list.get(9).toString());
			ordersInfo.setZhangdanleixing(list.get(10).toString());
			ordersInfo.setYunshufangshi(list.get(11).toString());
			ordersInfo.setWupinleixing(list.get(12).toString());
			orders.setAddress(list.get(13).toString());
			ordersInfo.setAddress(list.get(13).toString());
			orders.setCitys(list.get(14).toString());
			ordersInfo.setCitys(list.get(14).toString());
			ordersInfo.setJiesuanzhongliang(list.get(15).toString());
			ordersInfo.setShoujianzhongliang(list.get(16).toString());
			orders.setZhongxinzhongliang(list.get(17).toString());
			ordersInfo.setZhongxinzhongliang(list.get(17).toString());
			orders.setPaijianwangdian(list.get(18).toString());
			ordersInfo.setPaijianwangdian(list.get(18).toString());
			orders.setQianshouwangdian(list.get(19).toString());
			ordersInfo.setQianshouwangdian(list.get(19).toString());
			orders.setQianshoushijian(list.get(20).toString());
			ordersInfo.setQianshoushijian(list.get(20).toString());
			ordersInfo.setMiandanfei(list.get(21).toString());
			ordersInfo.setKehuyunfei(list.get(22).toString());
			ordersInfo.setKehufujiafei(list.get(23).toString());
			ordersInfo.setZhongzhuanfei(list.get(24).toString());
			ordersInfo.setCaozuofei(list.get(25).toString());
			ordersInfo.setJunzhongtongpiaofei(list.get(26).toString());
			ordersInfo.setJunzhongjiashoufei(list.get(27).toString());
			ordersInfo.setShougongyunfei(list.get(28).toString());
			ordersInfo.setYewuyuanticheng(list.get(29).toString());
			ordersInfo.setSuodingfeiyong(list.get(30).toString());
			ordersInfo.setZhongxinshouzhongzhuanfei(list.get(31).toString());
			ordersInfo.setZhongzhuanfeitiaozhengcishu(list.get(32).toString());
			ordersInfo.setXuzhongpaifei(list.get(33).toString());
			ordersInfo.setJichupaifei(list.get(34).toString());
			ordersInfo.setDaofukuan(list.get(35).toString());
			ordersInfo.setDaishoukuan(list.get(36).toString());
			ordersInfo.setTuijianbiaoshi(list.get(37).toString());
			ordersInfo.setWentijian(list.get(38).toString());
			ordersInfo.setMiandanleixing(list.get(39).toString());
			ordersInfo.setShujulaiyuan(list.get(40).toString());
			ordersInfo.setYundanfafang(list.get(41).toString());
			ordersInfo.setKoukuanshijian(list.get(42).toString());
			ordersInfo.setKoukuanren(list.get(43).toString());
			ordersInfo.setShoucikoukuanjine(list.get(44).toString());
			ordersInfo.setXiugaishijian(list.get(45).toString());
			ordersInfo.setXiugairen(list.get(46).toString());
			ordersInfo.setSuodingjiesuoshijian(list.get(47).toString());
			ordersInfo.setSuodingsuodingren(list.get(48).toString());
			ordersInfo.setDingdanlaiyuan(list.get(49).toString());
			ordersInfo.setBeizhu(list.get(50).toString());
		

			
			JSONObject jiage = YD_ShanghuJiage.getJiageByshids(id,orders.getAddress() );
			if (jiage.getString("error").equals("1")) {
				System.out.println(jiage.getString("msg"));
			}
			System.out.println(jiage+"第"+j+"条"+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
			JSONObject shanghujiage = JSONObject.parseObject(jiage.getString("data"));
		     
			BigDecimal rr ;
			if(shanghujiage==null){
				rr=new BigDecimal(5);
			}else{
			// 计算每条订单的应结款
		
			if (Double.valueOf(orders.getZhongxinzhongliang()) <= Double.valueOf(shanghujiage.getString("weight"))||Double.valueOf(orders.getZhongxinzhongliang())==null) {
				 rr=UtsBigdeciArith.add(new BigDecimal(shanghujiage.getString("saomiaofei")), new BigDecimal(shanghujiage.getString("qibujia")));;
			} else {
				BigDecimal zz = UtsBigdeciArith.sub(new BigDecimal(orders.getZhongxinzhongliang()),new BigDecimal(shanghujiage.getString("weight")));
				BigDecimal vv = UtsBigdeciArith.add(UtsBigdeciArith.mul(zz.setScale(0, BigDecimal.ROUND_UP),new BigDecimal(shanghujiage.getString("money"))),new BigDecimal(shanghujiage.getString("qibujia")));
				rr=UtsBigdeciArith.add(new BigDecimal(shanghujiage.getString("saomiaofei")), vv);
			}
		}
			
			//把其它几个添加的字段添加到数据库
			orders.setYingjiekuan(String.valueOf(rr));
			orders.setGlyid(users_token_sys.getString("id"));
			orders.setGlyname(users_token_sys.getString("name"));
			orders.setGlyimg(users_token_sys.getString("img"));
			orders.setMerchantid(yd_shanghu_id.getString("id"));
			orders.setMerchantname(yd_shanghu_id.getString("name"));
			orders.setMerchantimg(yd_shanghu_id.getString("img"));
			if (!ordersService.insert(orders)) {
				msglist.add("添加订单表添加失败"+j+";");
			}

			if (!ordersInfoService.insert(ordersInfo)) {
				msglist.add("订单表详情表添加数据失败"+j+";");
			}
			//更新余额表的应结款
			Balance balance=balanceService.selectOne(new EntityWrapper<Balance>().eq("usersid", id));
			
			balance.setYingjiekuan(String.valueOf(UtsBigdeciArith.add(new BigDecimal(balance.getYingjiekuan()),rr)));
			if (!balanceService.updateById(balance)) {
				msglist.add("添加余额表应结款添加失败"+j+";");
			}
		
	}
		
		
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		

	}

}
